﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Prism;
using Prism.UnityContainerAdapter;
using Prism.Interfaces;
using System.Windows.Controls;
using Prism.Regions;
using Prism.Services;

namespace Client
{
    public class Bootstrapper : IDisposable
    {
        IUnityContainer container;

        internal void Initialize()
        {
            InitializeContainer();
            RegisterGlobalServices();
            RegisterRegions();
            InitializeShell();
            InitializeModules();
        }

        private void InitializeModules()
        {
            IModule employeeModule = new EmployeeModule.EmployeeModule(container);

            employeeModule.Initialize();
        }

        private void InitializeShell()
        {
            Shell shell = new Shell();
            shell.Show();
        }

        private void RegisterRegions()
        {

            container.RegisterType<IRegion<Panel>, PanelRegion>(); // what's this then?
        }

        private void RegisterGlobalServices()
        {
            container.RegisterInstance<IRegionManagerService>(new RegionManagerService());
        }

        private void InitializeContainer()
        {
            container = new UnityContainer();
            container.RegisterInstance<IUnityContainer>(container);
            // register that container as our IOC container of choice
            PrismContainerProvider.Provider = new UnityPrismContainer(container);
        }

        // This is a breaking change to existing consumers
        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                container.Dispose();
            }
        }

        #endregion
    }
}
